﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class TopMostController : Controller
    {
        //
        // GET: /TopMost/
        Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetJsonTopMost()
        {
            var topproductlang = from top in db.TopMostProductLangs where top.LangID == Request.Cookies["_culture"]["lang"] select new { top.LangID, top.Image, top.DisplayName, top.Description, top.TopMostProduct.ProductID, top.TopMostID,top.TopMostProduct.Position};
            return Json(topproductlang,JsonRequestBehavior.AllowGet);
        }
        public ActionResult EditTopMost(int ID)
        {
            var topproductlang = (from top in db.TopMostProductLangs where top.LangID == Request.Cookies["_culture"]["lang"]&&top.TopMostID==ID select new ViewModel.TopMostViews {LangID= top.LangID,Image= top.Image,DisplayName= top.DisplayName, Description= top.Description,ProductID= top.TopMostProduct.ProductID,ID= top.TopMostID,Position= top.TopMostProduct.Position }).FirstOrDefault();
            topproductlang.Description = HttpUtility.HtmlDecode(topproductlang.Description);
            var listproduct = db.ProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]);
            ViewBag.ProductList = new SelectList(listproduct, "ProductID", "DisplayName", topproductlang.ProductID);
            return View(topproductlang);
        }
        [HttpPost,ValidateInput(false)]
        public ActionResult EditTopMost(ViewModel.TopMostViews model)
        {
            if (ModelState.IsValid)
            {
                var topproductlang = (from top in db.TopMostProductLangs where top.LangID == Request.Cookies["_culture"]["lang"] && top.TopMostID == model.ID select top).FirstOrDefault();
                if (topproductlang != null)
                {
                    topproductlang.Description = HttpUtility.HtmlEncode(model.Description);
                    topproductlang.DisplayName = model.DisplayName;
                    topproductlang.Image = model.Image;
                    topproductlang.TopMostProduct.Position = model.Position;
                    db.SubmitChanges();
                    return RedirectToAction("Index");
                }
            }
            return View();
        }
        [HttpPost]
        public ActionResult DeleteTopMostID(int ID)
        {
            try
            {
                var topproductlang = (from top in db.TopMostProductLangs where  top.TopMostID == ID select top);
                db.TopMostProductLangs.DeleteAllOnSubmit(topproductlang);
                db.SubmitChanges();
                var topmost = db.TopMostProducts.Where(m => m.ID == ID).FirstOrDefault();
                db.TopMostProducts.DeleteOnSubmit(topmost);
                db.SubmitChanges();

                return Json(new { success=true});
            }
            catch {
                return Json(new { success = false });
            }
        }

        public ActionResult AddTopMost()
        {
            var listproduct = db.ProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]);
            ViewBag.ProductList = new SelectList(listproduct, "ProductID", "DisplayName");
            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult AddTopMost(ViewModel.TopMostViews model)
        {
            if (ModelState.IsValid)
            {
                Models.TopMostProduct most = new Models.TopMostProduct();
                most.Position = model.Position;
                most.ProductID = model.ProductID;
                most.ProductName = model.DisplayName;
                db.TopMostProducts.InsertOnSubmit(most);
                db.SubmitChanges();

                Models.TopMostProductLang langvi = new Models.TopMostProductLang();
                langvi.LangID = "vi-VN";
                langvi.Image = model.Image;
                langvi.DisplayName = model.DisplayName;
                langvi.Description = HttpUtility.HtmlEncode(model.Description);
                langvi.TopMostID = most.ID;
                db.TopMostProductLangs.InsertOnSubmit(langvi);
                db.SubmitChanges();
                Models.TopMostProductLang langen = new Models.TopMostProductLang();
                langen.LangID = "en-US";
                langen.Image = model.Image;
                langen.DisplayName = model.DisplayName;
                langen.Description = HttpUtility.HtmlEncode(model.Description);
                langen.TopMostID = most.ID;
                db.TopMostProductLangs.InsertOnSubmit(langen);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
    }
}
